import tkinter as tk
import time

#   编码 {'尾巴':21}是{'基因':'尾巴','优先级':21,'权重':80}的简写
new_a = []
# 细胞的分裂
def quick_cell():
    try:
        # 返回值为列表
        dna = eval("([" + dna_text.get("1.0", "end-1c") + "])")
        quick(dna)
    except Exception as e:
        alert_label.config(text="请检查一下您的编码格式！")

# 快速排序
def quick(arr):
    global new_a
    if len(arr) < 2:
        return arr
    else:
        # 找到基准数（即第一个元素的值）
        pivot = int(list(arr[0].values())[0])
        # 小于等于标准值的数组
        less = [el for el in arr[1:] if int(list(el.values())[0]) <= pivot]
        # 大于标准值的数组
        greater = [el for el in arr[1:] if int(list(el.values())[0]) > pivot]
        # 返回别表方便计算
        new_a =  quick(less)  + [arr[0]] + quick(greater)
        result_str = "当前排序："
        # 循环遍历并显示当前结果
        for val in new_a:
            result_str += str(val) + ","
        print(result_str)
        # 休眠1秒钟
        time.sleep(1)
        return new_a
    

# 创建主窗口
root = tk.Tk()
root.title("鸡蛋的孵化（细胞的分裂）")

# 创建文本框
dna_text = tk.Text(root, width=50, height=10)
dna_text.insert(1.0,
"""{"心脏": 5},
{"心脏": 10},
{"眼睛": 6},
{"鼻子": 10},
{"鸟嘴": 13},
{"脚": 15},
{"翅膀": 19},
{"尾巴": 21}""")
dna_text.pack()

# 创建按钮
button = tk.Button(root, text="开始孵化", command=quick_cell)
button.pack()

# 创建提示标签
alert_label = tk.Label(root, text="")
alert_label.pack()

# 创建结果标签
result_label = tk.Label(root, text="")
result_label.pack()

# 运行主循环
root.mainloop()
